package se.rende.schema2ical.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import se.rende.schema2ical.client.JsonParser.ArrayParser;
import se.rende.schema2ical.client.JsonParser.MapPair;
import se.rende.schema2ical.client.JsonParser.MapParser;

public class Schema {
	private String startDate = "2010-12-12";
	private List<PassTime> passTimes = new ArrayList<PassTime>();
	private String passes = "";
	
	public Schema() {
	}
	

	public Schema(String jsonText) {
		parseJsonToSchema(jsonText, this);
	}
	
	public void parseJsonToSchema(String jsonText,	Schema schema) {
		JsonParser parser = new JsonParser(jsonText);
		MapParser mapParser = parser.getMapParser();
		while (mapParser.hasNext()) {
			MapPair pair = mapParser.getMapPair();
			if ("startDate".equals(pair.name)) {
				schema.setStartDate((String)pair.value);
			} else if ("passTimes".equals(pair.name)) {
				List<PassTime> passTimes = new ArrayList<PassTime>();
				ArrayParser ap = (ArrayParser) pair.value;
				while (ap.hasNext()) {
					MapParser mp = (MapParser) ap.get();
					String passName = (String) mp.getMapPair().value;
					String passStart = (String) mp.getMapPair().value;
					String passEnd = (String) mp.getMapPair().value;
					passTimes.add(new PassTime(passName.charAt(0), passStart, passEnd));
					mp.hasNext();
				}
				schema.setPassTimes(passTimes);
			} else if ("passes".equals(pair.name)) {
				schema.setPasses((String) pair.value);
			}
		}
	}

	public List<PassTime> getPassTimes() {
		return passTimes;
	}
	
	public void setPassTimes(List<PassTime> passTimes) {
		this.passTimes = passTimes;
	}
	
	public String getPasses() {
		return passes;
	}
	
	public void setPasses(String passes) {
		this.passes = passes;
	}
	
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getStartDate() {
		return startDate;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Schema([");
		boolean first = true;
		for (PassTime passTime : getPassTimes()) {
			if (!first) {
				sb.append(", ");
			}
			sb.append(passTime.getName());
			sb.append(":");
			sb.append(passTime.getFromTime() + "-" + passTime.getToTime());
			first = false;
		}
		sb.append("]");
		sb.append(", passes='" + passes + "')");
		return sb.toString();
	}

	public PassTime getPassTime(char ch) {
		for (PassTime passTime : passTimes) {
			if (ch == passTime.getName()) {
				return passTime;
			}
		}
		return null;
	}

	public int getPassCount() {
		Map<Character, PassTime> passTimeMap = new HashMap<Character, PassTime>();
		for (PassTime passTime : getPassTimes()) {
			passTimeMap.put(passTime.getName(), passTime);
		}
		int passCnt = 0;
		for (char ch : passes.toCharArray()) {
			if (passTimeMap.get(ch) != null) {
				passCnt++;
			} else if (ch == '-') {
				passCnt++;
			} else if (ch == '\r' || ch == '\n') {
				// whitespace
			} else {
				// error!
			}
		}
		return passCnt;
	}

}
